var path = "";
$(function(){
	path = $("#path").val();
});

var Main = {
		totalPage : 1,//总页数
		prePage : 1, //上一页
		nextPage : 1, //下一页
		pageNum : 1, //当前页
		param : null,
		requestAjax : function(url,param,fn){
			if(url == null || url === "" || typeof fn != "function"){
				alert("参数传递错误，正确方式:_requestAjax(url,{param1:param1,...},function(data))");
				return;
			}
			param = param || {};
			$.post(url,param,function(data){
				if(typeof (data) != "object" && data == "timeout"){
					alert("登录超时，请重新登录");
					wondow.location = path + "/login/";
				} else {
					fn(data);
				}
			});
		},
		
		ajaxPage : function(url,param,fn){
			if(url == null || url ==="" || typeof fn != "function"){
				alert("参数传递错误，正确方式：ajaxPage(url,{param1:param1,...},function(data){...})");
				return;
			}
			if(param == null){
				param = {};
			}
			
			param.pageNum = 1;
			Main.param = param;
			$.ajax({
				url : url,
				type : 'post',
				data : Main.param,
				dataType : 'json',
				success : function(data){
					Main.totalPage = data.totalPage;
					Main.pageNum = data.pageNum;
					Main.nextPage = data.nextPage;
					Main.prePage = data.prePage;
					Main.getPageBox();
					if (typeof data != "object"){
						alert("系统登录超时，请重新登陆。。");
						window.location = "/login/";
					} else {
						fn(data.data);
					}
				},
				error : function(XMLHttpRequest,textStatus,errorThrown){
					if (textStatus === "error"){
						alert("请求失败!!");
					} else if (textStatus == "timeout"){
						alert("请求超时!!");
					}
				}
			});
			// 先解除事件绑定防止重复执行
			$(document).off("click",".page_prev");
			$(document).off("click",".page_next");
			$(document).off("click","goPage");
			// 上一页
			$(document).on("click",".page_prev",function(){
				if(Main.prePage >= 1){
					Main.param.pageNum = Main.prePage;
					$.ajax({
						url : url,
						type : 'post',
						data : Main.param,
						timeout : 100000,
						dataType : 'json',
						success : function(data){
							Main.totalPage = data.totalPage;
							Main.pageNum = data.pageNum;
							Main.nextPage = data.nextPage;
							Main.prePage = data.prePage;
							Main.getPageBox();
							if (typeof (data) != "object") {
								if (status.indexOf("<html>") > 0) {
									alert("系统登录超时，请重新登录。。");
									window.location = "/login/";
								}
							} else {
								fn(data.data);
							}
						},
						error : function(XMLHttpRequest, textStatus, errorThrown) {
							if (textStatus === "error") {
								alert("请求失败！！");
							} else if (textStatus == "timeout") {
								alert("请求超时！！");
							}

						}
					});
				}
			});
			
			// 下一页
			$(document).on("click",".page_next",function(){
				if(Main.nextPage <= Main.totalPage){
					Main.param.pageNum = Main.nextPage;
					$.ajax({
						url : url,
						type : 'post',
						data : Main.param,
						timeout : 100000,
						dataType : 'json',
						success : function(data){
							Main.totalPage = data.totalPage;
							Main.pageNum = data.pageNum;
							Main.nextPage = data.nextPage;
							Main.prePage = data.prePage;
							Main.getPageBox();
							if (typeof (data) != "object") {
								if (status.indexOf("<html>") > 0) {
									alert("系统登录超时，请重新登录。。");
									window.location = "/login/";
								}
							} else {
								fn(data.data);
							}
						},
						error : function(XMLHttpRequest, textStatus, errorThrown) {
							if (textStatus === "error") {
								alert("请求失败！！");
							} else if (textStatus == "timeout") {
								alert("请求超时！！");
							}
						}
					});
				}
			});
			
			// 跳转页
			$(document).on("click",".goPage",function(){
				var pageNum = $(this).text();
				if(pageNum === ""){
					alert("页码不能为空!");
					return;
				}
				if(!isNaN(pageNum)){
					if(pageNum > Main.totalPage){
						alert("页码不能大于总页数!");
						return;
					}
					Main.param.pageNum = pageNum;
					$.ajax({
						url : url,
						type : 'post',
						data : Main.param,
						timeout : 100000,
						dataType : 'json',
						success : function(data){
							Main.totalPage = data.totalPage;
							Main.pageNum = data.pageNum;
							Main.nextPage = data.nextPage;
							Main.prePage = data.prePage;
							Main.getPageBox();
							if (typeof (data) != "object") {
								if (status.indexOf("<html>") > 0) {
									alert("系统登录超时，请重新登录。。");
									window.location = "/login/";
								}
							} else {
								fn(data.data);
							}
						},
						error : function(XMLHttpRequest, textStatus, errorThrown) {
							if (textStatus === "error") {
								alert("请求失败！！");
							} else if (textStatus == "timeout") {
								alert("请求超时！！");
							}

						}
					});
				}
			});
		},
		/**
		 * 动态地生成分页列表
		 */
		getPageElement : function(element,pageList){
			var pageBox = "<div class='page_box'>";
			pageBox += "<a href='javascript:;' class='page_prev'>上一页</a>";
			pageBox += pageList;
			pageBox += "<a href='javascript:;' class='page_next'>下一页</a>";
			pageBox += "</div>";
			$(document).find(element).empty().html(pageBox);
		},
		
		getPageBox : function(){
			var pageHtml = "";
			if (Main.pageNum - 2 >= 1) {
				for (var i = Main.pageNum - 2; i <= Main.pageNum; i++) {
					if (i == Main.pageNum) {
						pageHtml += '<a href="javascript:;" class="goPage current">'
								+ i + '</a>';
					} else {
						pageHtml += '<a href="javascript:;" class="goPage">' + i
								+ '</a>';
					}

				}
			} else {
				for (var i = 1; i <= Main.pageNum; i++) {
					if (i == Main.pageNum) {
						pageHtml += '<a href="javascript:;" class="goPage current">'
								+ i + '</a>';
					} else {
						pageHtml += '<a href="javascript:;" class="goPage">' + i
								+ '</a>';
					}

				}
			}
			if (Main.pageNum + 2 <= Main.totalPage) {
				for (var j = Main.pageNum + 1; j <= Main.pageNum + 2; j++) {
					if (j == Main.pageNum) {
						pageHtml += '<a href="javascript:;" class="goPage current">'
								+ j + '</a>';
					} else {
						pageHtml += '<a href="javascript:;" class="goPage">' + j
								+ '</a>';
					}

				}
			} else {
				for (var j = Main.pageNum + 1; j <= Main.totalPage; j++) {
					if (j == Main.pageNum) {
						pageHtml += '<a href="javascript:;" class="goPage current">'
								+ j + '</a>';
					} else {
						pageHtml += '<a href="javascript:;" class="goPage">' + j
								+ '</a>';
					}

				}
			}
			if (Main.totalPage - Main.pageNum <= 1 && Main.totalPage >= 5) {
				pageHtml = "";
				for (var j = Main.totalPage - 5; j <= Main.totalPage; j++) {
					if (j == Main.pageNum) {
						pageHtml += '<a href="javascript:;" class="goPage current">'
								+ j + '</a>';
					} else {
						pageHtml += '<a href="javascript:;" class="goPage">' + j
								+ '</a>';
					}
				}
			}
			if (Main.pageNum - 1 <= 1 && Main.totalPage >= 5) {
				pageHtml = "";
				for (var j = 1; j <= 5; j++) {
					if (j == Main.pageNum) {
						pageHtml += '<a href="javascript:;" class="goPage current">'
								+ j + '</a>';
					} else {
						pageHtml += '<a href="javascript:;" class="goPage">' + j
								+ '</a>';
					}
				}
			}

			Main.getPageElement(".ug_page_box", pageHtml);
		}
}